#!/usr/bin/python
import sys
import os

if len(sys.argv) <> 5:
	print 'score_to_aut_hitlist.py <sybyl-table.hits> <hitlist-in> <hitlist-out> <broken-list>'
	sys.exit()

s_sybyl = sys.argv[1]
s_hitlist = sys.argv[2]
s_hitout = sys.argv[3]
s_broken = sys.argv[4]
d_syb = {}
file = open(s_sybyl, 'r')
for i in file.readlines():
	if 'name="' in i:
		s_name = i.split('name="')[1].split('"')[0]
		d_syb[s_name] = {}
	if 'RANK=' in i:
		i_rank = int(i.split('RANK=')[1].split(';')[0])
		d_syb[s_name]['rank'] = i_rank
	if 'TOT_SCORE=' in i:
		f_tot = float(i.split('TOT_SCORE=')[1].split(';')[0])
		d_syb[s_name]['tot_score'] = f_tot
	if 'ELEC=' in i:
		f_elec = float(i.split('ELEC=')[1].split(';')[0])
		d_syb[s_name]['elec'] = f_elec
	if 'VDW=' in i:
		f_vdw = float(i.split('VDW=')[1].split(';')[0])
		d_syb[s_name]['vdw'] = f_vdw
	if 'POLSOL=' in i:
		f_pol = float(i.split('POLSOL=')[1].split(';')[0])
		d_syb[s_name]['polsol'] = f_pol
	if 'APOL=' in i:
		f_apol = float(i.split('APOL=')[1].split(';')[0])
		d_syb[s_name]['apolsol'] = f_apol
	if 'HEAV=' in i:
		i_heav = int(i.split('HEAV=')[1].split(';')[0])
		d_syb[s_name]['heav'] = i_heav
	if 'FUNCTIONAL_DATA=' in i:
		f_scpheav = float(i.split('FUNCTIONAL_DATA=')[1].split('>')[0])
		d_syb[s_name]['score/heav'] = f_scpheav
file.close
l_syb = d_syb.keys()
print len(l_syb), 'results in sybyl-table.'

l_broken = []
l_wrong = []
file = open(s_broken, 'r')
for i in file.readlines():
	if not 'rank' in i:
		i_rank = int(i.split()[0])
		s_name = i.split()[1]
		if d_syb.has_key(s_name):
			if d_syb[s_name]['rank'] <> i_rank:
				s_text = s_name + ' is not rank ' + str(i_rank)
				print s_text
				l_wrong.append(i_rank)
			else:
				l_broken.append(s_name)
				#s_command = 'mv pharm.mdb/' + s_name + '.mol2 pharm_broken/'
				#os.system(s_command)
				#print s_command
		if d_syb.has_key(s_name) == False or i_rank in l_wrong:
			for j in d_syb.keys():
				if d_syb[j]['rank'] == i_rank:
					l_broken.append(j)
					#print j, 'will be excluded.'
					#s_command = 'mv pharm.mdb/' + j + '.mol2 pharm_broken/'
					#os.system(s_command)
					#print s_command
file.close()

d_hits = {}
l_scores = []
file = open(s_hitlist, 'r')
for i in file.readlines():
    s_name = i.split()[0]
    if s_name in l_broken:
	print s_name, 'broken, excluded from list.'
    else:
	l_info = [str(int(i.split()[2]) + int(i.split()[4]))]
	l_comment = ['-']
	d_hits[s_name] = {'rank':d_syb[s_name]['rank']}
	d_hits[s_name]['info'] = l_info
	d_hits[s_name]['comment'] = l_comment
	d_hits[s_name]['tot_score'] = d_syb[s_name]['tot_score']
	d_hits[s_name]['score/heav'] = d_syb[s_name]['score/heav']
	l_scores.append(d_syb[s_name]['score/heav'])
	d_hits[s_name]['elec'] = d_syb[s_name]['elec']
	d_hits[s_name]['vdw'] = d_syb[s_name]['vdw']
	d_hits[s_name]['polsol'] = d_syb[s_name]['polsol']
	d_hits[s_name]['apolsol'] = d_syb[s_name]['apolsol']
	d_hits[s_name]['rank_heav'] = 0
file.close()

file = open(s_hitout, 'w')
s_head = ' ID          rank    H-Br   tot_score   score/heav   rank_heav   elec      vdw       polsol   apolsol   comment\n'
file.write(s_head)
l_scores.sort()
i_counter = 1
f_last = 0.
for i in l_scores:
	for j in d_hits.keys():
		if d_hits[j]['score/heav'] == i and i <> f_last:
			d_hits[j]['rank_heav'] = i_counter
			s_comment = ''
			for k in range(len(d_hits[j]['comment'])):
				s_comment += d_hits[j]['comment'][k] + ' '
			s_line = ' %9.9s   %5.5s   %4.4s   %9.9s   %10.10s   %9.9s   %7.7s   %7.7s   %6.6s   %7.7s   %s\n' \
			%(j, d_hits[j]['rank'], d_hits[j]['info'][0], d_hits[j]['tot_score'], \
			str(i), i_counter, d_hits[j]['elec'], d_hits[j]['vdw'], d_hits[j]['polsol'], \
			d_hits[j]['apolsol'], s_comment)
			file.write(s_line)
			i_counter += 1
	f_last = i
file.close()



























